package controller

import (
	"admin/model"
	"common/helper"
	"encoding/json"
	"github.com/valyala/fasthttp"
	"time"
)

type StatisticsController struct{}

// 首页总统计
func (this *StatisticsController) TotalData(ctx *fasthttp.RequestCtx) {
	s, err := model.ReportPlatformTotalData(ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 日况统计概要
func (this *StatisticsController) DailyList(ctx *fasthttp.RequestCtx) {
	param := model.ReportDailyParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	param.IsOperator = ctx.QueryArgs().GetUintOrZero("is_operator")
	param.IsBusiness = ctx.QueryArgs().GetUintOrZero("is_business")

	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.ReportPlatformDailyList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 游戏盈亏结算
func (this *StatisticsController) GameSettle(ctx *fasthttp.RequestCtx) {
	param := model.GameDailyParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	param.IsOperator = ctx.QueryArgs().GetUintOrZero("is_operator")
	param.IsBusiness = ctx.QueryArgs().GetUintOrZero("is_business")
	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.ReportGameDailyList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 玩家游戏盈亏结算
func (this *StatisticsController) UserGameSettle(ctx *fasthttp.RequestCtx) {
	param := model.UserGameDailyParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.ReportUserGameDailyList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 游戏生态监控
func (this *StatisticsController) UserGameMonitor(ctx *fasthttp.RequestCtx) {
	param := model.UserGameMonitorParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.ReportGameMonitorList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 留存数据
func (this *StatisticsController) UserRemain(ctx *fasthttp.RequestCtx) {
	param := model.UserRemainParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.ReportUserRemainList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 渠道对账表
func (this *StatisticsController) MerchantSettle(ctx *fasthttp.RequestCtx) {
	param := model.ReportApiFeeParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	param.IsOperator = ctx.QueryArgs().GetUintOrZero("is_operator")
	param.IsBusiness = ctx.QueryArgs().GetUintOrZero("is_business")
	param.ExportExcel = ctx.QueryArgs().GetUintOrZero("export_excel")

	if param.ExportExcel == 1 {
		param.Page = 1
		param.PageSize = model.ExportExcelPageSize
	}

	if param.Page < 1 {
		param.Page = 1
	}

	data, err := model.ReportApiFeeList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	if param.ExportExcel == 1 {
		exportData := [][]interface{}{}
		for _, value := range data.D {
			single := []interface{}{
				value.OperatorId,
				value.OperatorName,
				value.Deposit,
				value.DepositFee,
				value.Withdraw,
				value.WithdrawFee,
				value.ApiFee,
				value.Fagamimg,
				value.TotalProfit,
			}
			exportData = append(exportData, single)
		}
		exportTitle := []string{
			"渠道ID",
			"渠道名称",
			"总充值",
			"充值手续费",
			"总提现",
			"提现手续费",
			"API费用",
			"假pg盈亏",
			"总利润",
		}
		filename := "渠道对账表-" + time.Now().Format("20060102150405")
		if model.ExportExcel(filename, exportTitle, exportData) == nil {
			helper.RetData(ctx, true, filename)
		} else {
			helper.RetData(ctx, false, helper.RequestFail)
		}
		return
	} else {
		helper.RetData(ctx, true, data)
		return
	}

}
